using System;
using System.Collections.Generic;
using ProtoModels;

public class PlayedGamesManager : SingletonInit<PlayedGamesManager>
{
	private const string Filename = "mp.bytes";

	protected PlayedGamesTracker model;

	public override void Init()
	{
		if (!ProtoIO.LoadFromFile<ProtoSerializer, PlayedGamesTracker>("mp.bytes", GGFileIO.instance, out model))
		{
			model = new PlayedGamesTracker();
			ProtoIO.SaveToFile<ProtoSerializer, PlayedGamesTracker>("mp.bytes", GGFileIO.instance, model);
		}
	}

	public void Save()
	{
		ProtoIO.SaveToFileCloudSync("mp.bytes", model);
	}

	public TimeSpan TimeSpanTillResetPlayedGame()
	{
		return TimeSpan.FromDays(1.0);
	}

	public bool IsTimeOk(PlayedGamesTracker.PlayedGame game)
	{
		return IsTimeOk(TimeSpanTillResetPlayedGame(), game);
	}

	public bool IsTimeOk(TimeSpan maxTimeBeforePlayed, PlayedGamesTracker.PlayedGame game)
	{
		DateTime dateTime = new DateTime(game.timePlayed) + maxTimeBeforePlayed;
		return dateTime > DateTime.UtcNow;
	}

	public bool IsGamePlayed(string playerId)
	{
		return IsGamePlayed(playerId, TimeSpanTillResetPlayedGame());
	}

	public bool IsGamePlayed(string playerId, TimeSpan maxTimeBeforePlayed)
	{
		if (model.playedGames == null)
		{
			return false;
		}
		List<PlayedGamesTracker.PlayedGame> playedGames = model.playedGames;
		for (int i = 0; i < playedGames.Count; i++)
		{
			PlayedGamesTracker.PlayedGame playedGame = playedGames[i];
			if (!(playerId != playedGame.opponentId))
			{
				DateTime dateTime = new DateTime(playedGame.timePlayed) + maxTimeBeforePlayed;
				return IsTimeOk(maxTimeBeforePlayed, playedGame);
			}
		}
		return false;
	}

	public PlayedGamesTracker.PlayedGame GetGameForOpponentId(string opponentId)
	{
		if (string.IsNullOrEmpty(opponentId))
		{
			return null;
		}
		List<PlayedGamesTracker.PlayedGame> playedGames = model.playedGames;
		if (playedGames == null)
		{
			return null;
		}
		for (int i = 0; i < playedGames.Count; i++)
		{
			PlayedGamesTracker.PlayedGame playedGame = playedGames[i];
			if (!(opponentId != playedGame.opponentId))
			{
				return playedGame;
			}
		}
		return null;
	}

	public void RemoveStaleGames(TimeSpan minTimeBeforeStale)
	{
		List<PlayedGamesTracker.PlayedGame> list = new List<PlayedGamesTracker.PlayedGame>();
		List<PlayedGamesTracker.PlayedGame> playedGames = model.playedGames;
		if (playedGames == null)
		{
			return;
		}
		for (int i = 0; i < playedGames.Count; i++)
		{
			PlayedGamesTracker.PlayedGame playedGame = playedGames[i];
			if (!IsTimeOk(minTimeBeforeStale, playedGame))
			{
				list.Add(playedGame);
			}
		}
		List<PlayedGamesTracker.PlayedGame> list2 = list;
		for (int j = 0; j < list2.Count; j++)
		{
			PlayedGamesTracker.PlayedGame item = list2[j];
			model.playedGames.Remove(item);
		}
	}

	public void OnGamePlayed(string opponentId, int room)
	{
		RemoveStaleGames(TimeSpanTillResetPlayedGame());
		PlayedGamesTracker.PlayedGame playedGame = GetGameForOpponentId(opponentId);
		if (playedGame == null)
		{
			playedGame = new PlayedGamesTracker.PlayedGame();
			if (model.playedGames == null)
			{
				model.playedGames = new List<PlayedGamesTracker.PlayedGame>();
			}
			model.playedGames.Add(playedGame);
		}
		playedGame.opponentId = opponentId;
		playedGame.room = room;
		playedGame.timePlayed = DateTime.UtcNow.Ticks;
		Save();
	}
}
